/*
 * MultiPhase.cpp
 *
 *  Created on: 21 Jul 2011
 *      Author: Allan
 */

#include "MultiPhase.h"

MultiPhase::MultiPhase() :
isOpen(false)
{}

MultiPhase::~MultiPhase() 
{}

MultiPhase::MultiPhase(const vector<PhasePtr>& phases) :
phases(phases)
{
	// Loop over all the added phases to set the sequence of species 
	BOOST_FOREACH(const PhasePtr& phase, phases)
		species.insert(species.end(), phase->GetSpecies().begin(), phase->GetSpecies().end());
}

const vector<PhasePtr>& MultiPhase::GetPhases() const
{
	return phases;
}

const StringVector& MultiPhase::GetSpecies() const
{
	return species;
}

const Index MultiPhase::GetSpeciesIndex(const String& speciesName) const
{
	return std::find(species.begin(), species.end(), speciesName) - species.begin();
}

const IndexVector MultiPhase::GetSpeciesIndexes(const StringVector& speciesNames) const
{
	IndexVector indexes; indexes.reserve(speciesNames.size());
	
	BOOST_FOREACH(const String& s, speciesNames)
		indexes.push_back((*this)[s]);
	
	return indexes;
}

const Index MultiPhase::operator[](const String& speciesName) const
{
	return GetSpeciesIndex(speciesName);
}

const IndexVector MultiPhase::operator[](const StringVector& speciesNames) const
{
	return GetSpeciesIndexes(speciesNames);
}

const DoubleVector MultiPhase::ActivityCoefficients(double T, double P, const DoubleVector& n) const
{
	auto activityCoefficientsFunction = 
			[](const PhasePtr& phase, double T, double P, const DoubleVector& n) { return phase->ActivityCoefficients(T, P, n); }; 
	
	return GenericPhaseComputation(T, P, n, activityCoefficientsFunction);
}

const DoubleVector MultiPhase::Activities(double T, double P, const DoubleVector& n) const
{
	auto activitiesFunction = 
			[](const PhasePtr& phase, double T, double P, const DoubleVector& n) { return phase->Activities(T, P, n); };
	
	return GenericPhaseComputation(T, P, n, activitiesFunction);
}

//	/// --- Add methods --- ///
//	void
//	Open();
//	
//	bool
//	AddPhase(const GenericPhase& phase);
//	
//	void
//	Close();

//void MultiPhase::Open()
//{
//	isOpen = true;
//}
//	
//bool MultiPhase::AddPhase(const GenericPhase& phase)
//{
//	if(isOpen) phases.push_back( phase.Clone() );
//	
//	return isOpen;
//}
//
//void MultiPhase::Close()
//{
//	// Loop over all the added phases to set the sequence of species 
//	BOOST_FOREACH(const GenericPhase& phase, this->GetPhases())
//		species.insert(species.end(), phase.GetSpecies().begin(), phase.GetSpecies().end());
//	
//	// Close the object for phase addition
//	isOpen = false;
//}
